package com.judian.smart.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class JSONFileUtils {
    public static final String TAG = JSONFileUtils.class.getSimpleName();

    public static void saveJSONToFile(File f, String json) {
        try {
            if (!f.exists()) {
                boolean status = f.createNewFile();
                Log.d(TAG, "saveJSONToFile: 创建文件状态" + status);
            }
            FileWriter writer = new FileWriter(f);
            writer.write(json, 0, json.length());
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String getJsonStringFromFile(File f) {
        StringBuilder readStr = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            if (f.exists()) {
                bufferedReader = new BufferedReader(new FileReader(f), 10);
                String len;
                while ((len = bufferedReader.readLine()) != null) {
                    readStr.append(len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return readStr.toString();

    }

}
